/**
 * FeatureWarpingFilterControl.java
 * 
 * @date: 2012-03-09
 * @author: Xiaoyu Guo (MUN# 200982155)
 * This file is part of the course project for COMP 6752, 
 * Application of Computer Graphics. All the file is written
 * by myself.
 */
package mun.cs6752.project.filter.featureWarping;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import mun.cs6752.project.filter.abstractClasses.AbstractImageFilterControl;
import mun.cs6752.project.filter.featureWarping.FeatureWarpingParameter.LineSegment;
import mun.cs6752.project.filter.interfaces.IImageFilter;
import mun.cs6752.project.filter.interfaces.IImageFilterParameter;
import mun.cs6752.project.filter.interfaces.IImageProvider;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JList;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;

/**
 * @author Xiaoyu Guo
 * @param <LineSegment>
 *
 */
public class FeatureWarpingFilterControl extends AbstractImageFilterControl {

    private static final long serialVersionUID = -4827968986017234813L;

    /**
     * @param provider
     * @param filter
     */
    public FeatureWarpingFilterControl(IImageProvider provider, IImageFilter filter) {
        super(provider, filter);
    }
    
    private List<LineSegment> srcLines = new ArrayList<LineSegment>();
    private List<LineSegment> destLines = new ArrayList<LineSegment>();
    private double a;
    private double b;
    private double c;
    private JSpinner spA;
    private JSpinner spB;
    private JSpinner spC;

    @Override
    protected void initUI(){
        super.initUI();
        
        JPanel pnlControls = new JPanel();
        this.add(pnlControls, BorderLayout.CENTER);
        GridBagLayout gbl_pnlControls = new GridBagLayout();
        gbl_pnlControls.columnWidths = new int[]{0, 0, 0};
        gbl_pnlControls.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_pnlControls.columnWeights = new double[]{1.0, 1.0, 0.0};
        gbl_pnlControls.rowWeights = new double[]{0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE, 0.0, 0.0};
        pnlControls.setLayout(gbl_pnlControls);
        
        JLabel lblControlLines = new JLabel("Control Lines");
        GridBagConstraints gbc_lblControlLines = new GridBagConstraints();
        gbc_lblControlLines.anchor = GridBagConstraints.WEST;
        gbc_lblControlLines.insets = new Insets(0, 0, 5, 5);
        gbc_lblControlLines.gridx = 0;
        gbc_lblControlLines.gridy = 0;
        pnlControls.add(lblControlLines, gbc_lblControlLines);
        
        JList lstLines = new JList(new ControlLineSegmentListModel(srcLines));
        lstLines.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        GridBagConstraints gbc_lstLines = new GridBagConstraints();
        gbc_lstLines.fill = GridBagConstraints.BOTH;
        gbc_lstLines.insets = new Insets(0, 0, 5, 5);
        gbc_lstLines.gridwidth = 2;
        gbc_lstLines.anchor = GridBagConstraints.NORTHWEST;
        gbc_lstLines.gridheight = 2;
        gbc_lstLines.gridx = 0;
        gbc_lstLines.gridy = 1;
        pnlControls.add(lstLines, gbc_lstLines);
        
        JButton btnAddLine = new JButton("+");
        GridBagConstraints gbc_btnAddLine = new GridBagConstraints();
        gbc_btnAddLine.insets = new Insets(0, 0, 5, 0);
        gbc_btnAddLine.gridx = 2;
        gbc_btnAddLine.gridy = 1;
        pnlControls.add(btnAddLine, gbc_btnAddLine);
        
        JButton btnRemoveLine = new JButton("-");
        GridBagConstraints gbc_btnRemoveLine = new GridBagConstraints();
        gbc_btnRemoveLine.insets = new Insets(0, 0, 5, 0);
        gbc_btnRemoveLine.gridx = 2;
        gbc_btnRemoveLine.gridy = 2;
        pnlControls.add(btnRemoveLine, gbc_btnRemoveLine);
        
        JLabel lblArguments = new JLabel("Arguments");
        GridBagConstraints gbc_lblArguments = new GridBagConstraints();
        gbc_lblArguments.anchor = GridBagConstraints.WEST;
        gbc_lblArguments.insets = new Insets(0, 0, 5, 5);
        gbc_lblArguments.gridx = 0;
        gbc_lblArguments.gridy = 3;
        pnlControls.add(lblArguments, gbc_lblArguments);
        
        JLabel lblA = new JLabel("a");
        GridBagConstraints gbc_lblA = new GridBagConstraints();
        gbc_lblA.insets = new Insets(0, 0, 5, 5);
        gbc_lblA.gridx = 0;
        gbc_lblA.gridy = 4;
        pnlControls.add(lblA, gbc_lblA);
        
        this.spA = new JSpinner();
        spA.setModel(new SpinnerNumberModel(new Double(0.0001), new Double(0), null, new Double(0.00002)));
        GridBagConstraints gbc_spA = new GridBagConstraints();
        gbc_spA.gridwidth = 2;
        gbc_spA.fill = GridBagConstraints.HORIZONTAL;
        gbc_spA.insets = new Insets(0, 0, 5, 5);
        gbc_spA.gridx = 1;
        gbc_spA.gridy = 4;
        pnlControls.add(spA, gbc_spA);
        
        JLabel lblB = new JLabel("b");
        GridBagConstraints gbc_lblB = new GridBagConstraints();
        gbc_lblB.insets = new Insets(0, 0, 5, 5);
        gbc_lblB.gridx = 0;
        gbc_lblB.gridy = 5;
        pnlControls.add(lblB, gbc_lblB);
        
        this.spB = new JSpinner();
        spB.setModel(new SpinnerNumberModel(new Double(1), null, null, new Double(0.01)));
        GridBagConstraints gbc_spB = new GridBagConstraints();
        gbc_spB.gridwidth = 2;
        gbc_spB.fill = GridBagConstraints.HORIZONTAL;
        gbc_spB.insets = new Insets(0, 0, 5, 5);
        gbc_spB.gridx = 1;
        gbc_spB.gridy = 5;
        pnlControls.add(spB, gbc_spB);
        
        JLabel lblNewLabel = new JLabel("c");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 6;
        pnlControls.add(lblNewLabel, gbc_lblNewLabel);
        
        this.spC = new JSpinner();
        spC.setModel(new SpinnerNumberModel(new Double(2), null, null, new Double(0.01)));
        GridBagConstraints gbc_spC = new GridBagConstraints();
        gbc_spC.gridwidth = 2;
        gbc_spC.fill = GridBagConstraints.HORIZONTAL;
        gbc_spC.insets = new Insets(0, 0, 0, 5);
        gbc_spC.gridx = 1;
        gbc_spC.gridy = 6;
        pnlControls.add(spC, gbc_spC);
    }
    
    @Override
    protected void initValue() {
        setA(0.0001);
        setB(1.0);
        setC(2.0);
    }

    private void setA(double a) {
        if(this.a != a){
            this.a = a;
            this.spA.setValue(a);
        }
    }

    private void setB(double b) {
        if(this.b != b){
            this.b = b;
            this.spB.setValue(b);
        }
    }

    private void setC(double c) {
        if(this.c != c){
            this.c = c;
            this.spC.setValue(c);
        }
    }
    
    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilterControl#getParameter()
     */
    @Override
    public IImageFilterParameter getParameter() {
        FeatureWarpingParameter result = new FeatureWarpingParameter();
        result.setSrcLines(srcLines);
        result.setDestLines(destLines);
//        result.setA(this.a);
//        result.setB(this.b);
//        result.setC(this.c);
        return result;
    }
}
